package com.epam.training.service.bonus;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.epam.training.domain.Bonus;
import com.epam.training.repository.BonusRepository;

@Service
@Transactional
public class BonusServiceImpl implements BonusService {
	private static final int PAGE_SIZE = 10;

	@Autowired
	BonusRepository repository;

	@Override
	public List<Bonus> getAllBonus() {
		return repository.findAll();
	}

	@Override
	public Page<Bonus> getAllBonus(Integer pageNumber) {
		PageRequest pageRequest = new PageRequest(pageNumber - 1, PAGE_SIZE);
		return repository.findAll(pageRequest);
	}

	@Transactional
	@Override
	public void save(Bonus bonus) {
		repository.save(bonus);
	}

	@Override
	public Bonus getById(Integer id) {
		return repository.findOne(id);
	}

	@Override
	public void delete(Integer id) {
		repository.delete(id);
	}

	@Override
	public void update(Bonus bonus, Integer id) {
		Bonus bonusDB = repository.findOne(id);
		bonusDB.setName(bonus.getName());
		bonusDB.setAmount(bonus.getAmount());
		bonusDB.setDescription(bonus.getDescription());
		
		repository.save(bonus);
	}

}
